<?php

include_once('ConnectionManager.php');

function selectAll() {
    $query = "SELECT * FROM INTRODUCTION ORDER BY INT_CONTENT ASC";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function countByName($keyword) {
    $select = "SELECT INT_ID ";
    $from = "FROM INTRODUCTION ";
    $where = "WHERE INT_CONTENT LIKE '" . $keyword . "'";
    ConnectionManager::getInstance()->setQuery($select . $from . $where);
    ConnectionManager::getInstance()->query();
    return ConnectionManager::getInstance()->getNumRows();
}

function delete($id) {
    $sql = "DELETE FROM INTRODUCTION WHERE INT_ID = " . $id;
    ConnectionManager::getInstance()->setQuery($sql);
    return ConnectionManager::getInstance()->query();
}

function loadIntroductionByID($insID) {
    $query = "SELECT * FROM INTRODUCTION WHERE INT_ID = $insID";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function countTotalIntroduction() {
    $query = "SELECT INT_ID FROM INTRODUCTION";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return ConnectionManager::getInstance()->getNumRows();
}

function update($insID, $name) {
    $sql = "UPDATE INTRODUCTION 
                SET INT_CONTENT = '$name'
                WHERE INT_ID = $insID";
    ConnectionManager::getInstance()->setQuery($sql);
    return ConnectionManager::getInstance()->query();
}

function insert($name) {
    $sql = "INSERT INTO INTRODUCTION (INT_CONTENT) 
                VALUES ('$name')";
    ConnectionManager::getInstance()->setQuery($sql);
    return ConnectionManager::getInstance()->query();
}

function findBy($keyword, $start_position, $row_per_page) {
    $select = "SELECT INT_ID, INT_CONTENT FROM INTRODUCTION ";
    $where = "";


    if (isset($keyword)) {
        $where .= "WHERE INT_CONTENT LIKE '%$keyword%'";
    }

    $where .= " LIMIT " . $start_position . ", " . $row_per_page;
    ConnectionManager::getInstance()->setQuery($select . $where);
    return ConnectionManager::getInstance()->query();
}

function findFirstRowOfIntroduction(){
	$sql = "SELECT * FROM INTRODUCTION LIMIT 0,1";
	ConnectionManager::getInstance()->setQuery($sql);
	return ConnectionManager::getInstance()->query();
}

?>